<template>
  <div class="nav-bar">
    <ul class="nav-list">
      <router-link tag="li" class="nav-list-item active" to="home">
        <i class="nbicon nblvsefenkaicankaoxianban-1"></i>
        <span>首页</span>
      </router-link>
      <router-link tag="li" class="nav-list-item" to="category">
        <i class="nbicon nbfenlei"></i>
        <span>分类</span>
      </router-link>
      <router-link tag="li" class="nav-list-item" to="cart">
        <i><van-icon name="shopping-cart-o" :badge="String(count || '')" /></i>
        <span>购物车</span>
      </router-link>
      <router-link tag="li" class="nav-list-item" to="user">
        <i class="nbicon nblvsefenkaicankaoxianban-"></i>
        <span>我的</span>
      </router-link>
    </ul>
  </div>
</template>
<script setup>
import { onMounted, watch } from "vue";
import { useRoute } from "vue-router";
import { useCartStore } from "@/stores/cart.js";
import { storeToRefs } from "pinia";
import { getLocal } from "@/common/js/utils";
const cartStore = useCartStore();
const { count } = storeToRefs(cartStore);
const route = useRoute();
onMounted(() => {
  // 判断是否有token，如果有则更新购物车数量
  const token = getLocal("token");
  //获取当前路由路径
  const path = route.path;
  //首页和分类不需要更新购物车数量
  if (token) {
    cartStore.updateCart();
  }
});
//监听路由变化，如果不是首页和分类则更新购物车数量
watch(
  () => {
    route.path;
  },
  (newVal) => {
    if (newVal !== "/home" && newVal !== "/category") {
      cartStore.updateCart();
    }
  }
);
</script>

<style lang="less" scoped>
@import "../common/style/mixin";
.nav-bar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 5px 0;
  z-index: 1000;
  background: #fff;
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
  .nav-list {
    width: 100%;
    .fj();
    flex-direction: row;
    padding: 0;
    .nav-list-item {
      display: flex;
      flex: 1;
      flex-direction: column;
      text-align: center;
      color: #666;
      // 激活状态的颜色
      &.router-link-active {
        color: @primary;
      }
      i {
        text-align: center;
        font-size: 22px;
      }
      span {
        font-size: 12px;
      }
    }
  }
}
</style>
